/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmluebung2;

import java.util.Random;
import javax.swing.JPanel;

/**
 * Main Class responsible for initializing the frame and panels.
 * 
 * @author Adrian Cieluch
 */
public class XMLUebung2 {
    static int width = 1024, height = 768;
    static int panelShift = 10;
    static JPanel[] funktionen = new JPanel[4];
    
    /**
     * Main function checks the arguments for validity.
     * Creates the frame as well as the panels and connects the components.
     * 
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //at least one parameter exists
        if(args.length < 1)
            showInfoText();

        //parameter must be "funktionen"
        if(!args[0].equals("funktionen")) {
            showInfoText();
        }

        //second parameter exists
        if(args.length >= 2) {
            //second parameter is a number
            try {
                Integer.parseInt(args[1]);
            } catch (NumberFormatException e) {
                showInfoText();
            }

            //second parameter is withing bounds
            if(Integer.parseInt(args[1]) >= 1 && Integer.parseInt(args[1]) <= 11) {
                PanelManager.setColorScheme(Integer.parseInt(args[1]));
            } else {
                showInfoText();
            }
        } else { //if no second parameters exists, choose random colorscheme
            Random r = new Random();
            if(r.nextInt(2) == 0) {
                PanelManager.setColorScheme(r.nextInt(11));
            } else {
                PanelManager.setRandomScheme();
            }
        }
        
        Frame frame = new Frame("Uebung 1", width, height);
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel);
        panel.setLayout(null);
        
        funktionen[0] = PanelManager.setRandomPanel(panelShift, panelShift, width/2-panelShift*2, height/2-panelShift*2);
        funktionen[1] = PanelManager.setRandomPanel(width/2+panelShift, panelShift, width/2-panelShift*2, height/2-panelShift*2);
        funktionen[2] = PanelManager.setRandomPanel(panelShift, height/2+panelShift, width/2-panelShift*2, height/2-panelShift*2);
        funktionen[3] = PanelManager.setRandomPanel(width/2+panelShift, height/2+panelShift, width/2-panelShift*2, height/2-panelShift*2);
        
        for(int i = 0; i < 4; i++) {
            panel.add(funktionen[i]);
        }
    }
    
    /**
     * Shows an info text and terminates the program.
     */
    public static void showInfoText() {
        System.out.println("Bitte das Programm mit dem Parameter \"funktionen\" aufrufen. \nWahlweise kann "
                    + "ein zweiter Parameter (Zahl von 1 bis 11) angegeben werden zum Bestimmen eines Farbschemas");
        System.exit(1);
    }
}
